package pe.edu.upc.dsd.epica.movil;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import pe.edu.upc.dsd.epica.model.Usuario;
import pe.edu.upc.dsd.epica.util.Constants;
import pe.edu.upc.dsd.epica.util.RequestMethod;
import pe.edu.upc.dsd.epica.util.RestClient;
import pe.edu.upc.dsd.epica.util.TwitterUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class LoginActivity extends Activity {
	final String TAG = getClass().getName();
	
	 @Override
	 protected void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);

	  Bundle bund = this.getIntent().getExtras();
	  
	  TwitterUtils twtutils;
	  twtutils = new TwitterUtils();
	  
	  String login = "";
	  String tipo = "T";
	  
	  String token ;
	  String tokenSecret;

	  token = bund.getString("token");
	  tokenSecret = bund.getString("tokenSecret");
	  
	  login = twtutils.getNickName(token, tokenSecret);
	  
	  //mandar tweett
	  try {
		  twtutils.sendMessage(token, tokenSecret, "Hey!!!! En este momento estoy usando EpicaAndroid #epicaAndroid #epeupc ");
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}
		  
	  
	  RestClient client = new RestClient(Constants.UrlRest  + "usuario/loginTipo/" + login + "/" + tipo);

	  try {
	   client.Execute(RequestMethod.GET);
	  } catch (Exception e) {
		  Log.e(TAG, e.getMessage());
	  }

	  String response = client.getResponse();

	  Gson gson = new Gson();
	  Usuario responseObject = null;

	  try {
	   responseObject = gson.fromJson(response, Usuario.class);
	   Intent intent = new Intent(LoginActivity.this,
	     prom_ubica.class);   
	   startActivity(intent);
	  } catch (JsonParseException e) {
		  Log.e(TAG, e.getMessage());
	  }

	 }
	}